package com.kj.mynote.Activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.kj.mynote.Bean.NoteBean;
import com.kj.mynote.R;
import com.kj.mynote.Util.AutoUtils;
import com.kj.mynote.Util.FileUtils;
import com.kj.mynote.Util.ToolUtils;
import com.wang.avi.AVLoadingIndicatorView;

import org.litepal.crud.DataSupport;

import java.io.File;

/**
 * @author wy
 * @version V1.0
 * @Title: TimeNote
 * @Package com.kj.mynote.Activity
 * @Description: TODO(用一句话描述该文件做什么)
 * @date 17/3/24 14:52
 */

public class NoteReadActivity extends BaseActivity {
    EditText etNote;
    TextView btnTJ, tvSTime,tvETime, tvClass, tvTitle;
    ImageView back;
    AVLoadingIndicatorView loading;
    LinearLayout btnClass;
    ListView lv;

    int id;
    String path = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_note_edit);
        AutoUtils.auto(this);
        Intent intent = getIntent();
        id = intent.getIntExtra("id", -1);
        init();
        load();
        linstener();
    }

    private void init() {
        tvTitle = (TextView) findViewById(R.id.note_et_title);
        tvTitle.setText("查看便签");
        btnTJ = (TextView) findViewById(R.id.note_et_btn_tj);
        btnTJ.setVisibility(View.GONE);
        tvClass = (TextView) findViewById(R.id.note_et_tv_class);
        etNote = (EditText) findViewById(R.id.note_et);
        etNote.setEnabled(false);
        btnClass = (LinearLayout) findViewById(R.id.note_et_btn_class);
        back = (ImageView) findViewById(R.id.note_et_back);
        tvSTime = (TextView) findViewById(R.id.note_et_stime);
        tvETime = (TextView) findViewById(R.id.note_et_etime);
        loading = (AVLoadingIndicatorView) findViewById(R.id.note_et_loading);
    }

    private void load() {
        NoteBean note = DataSupport.where("id = ?", id + "").findFirst(NoteBean.class);
        path = note.getFilePath();
        tvSTime.setText(ToolUtils.TS2S(new Long(note.getStartTime()), "yyyy-MM-dd HH:mm"));
        tvETime.setText(ToolUtils.TS2S(new Long(note.getEndTime()), "yyyy-MM-dd HH:mm"));
        tvClass.setText(note.getNoteClass());
        new readFile().execute(note.getFilePath());
    }

    private void linstener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    class readFile extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            return FileUtils.readFile(new File(params[0]));
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.e("s",s);
            etNote.setText(s);
            loading.setVisibility(View.GONE);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading.setVisibility(View.VISIBLE);
        }
    }
}



